package routes

import (
	"go-gin-demo/controllers"
	"go-gin-demo/logger"
	"go-gin-demo/middlewares"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
)

func Setup(mode string) *gin.Engine {
	// 初始化 gin 的语言验证器
	if err := controllers.InitTrans("zh"); err != nil {
		zap.L().Error("init validator trans failed", zap.Error(err))
	}

	if mode == gin.ReleaseMode {
		gin.SetMode(mode)
	}
	r := gin.New()
	r.Use(logger.GinLogger(), logger.GinRecovery(true))

	// ping 测试服务通不通
	r.GET("/ping", middlewares.JWTAuthMiddleware(), func(c *gin.Context) {
		c.String(http.StatusOK, "ok")
	})

	// 注册用户路由
	r.POST("/signup", controllers.SignUpHandler)
	// 登录
	r.POST("/login", controllers.LoginHandler)

	// 更新 access token
	r.POST("/refresh_token", controllers.RefreshTokenHandler)

	// 没有路由默认 404
	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "404",
		})
	})

	return r
}
